package cn.cjy.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.cjy.model.User;
import cn.cjy.service.UserService;
import cn.cjy.service.impl.UserServiceImpl;

/**
 * Servlet implementation class StaticToLoginAndRegister
 */
@WebServlet("/AdminToLoginAndRegister")
public class AdminToLoginAndRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminToLoginAndRegister() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String ad=request.getParameter("ad");
		switch(ad) {
		case "tologin":
			response.sendRedirect("admin/index.jsp");
			break;
		case "register":
			response.sendRedirect("admin/tip.jsp");
			break;
		default:
			break;
		
		}
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String ad=request.getParameter("ad");
		switch(ad) {
		case "tologin":
			tologin(request,response);
			break;
		case "register":
			register(request,response);
			break;
		default:
			break;
		
		}
	}

	private void tologin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		UserService userService=new UserServiceImpl();
		User user=userService.login(username, password);
		if(user!=null&&user.getUserid()!=null) {
			RequestDispatcher dispatcher=request.getRequestDispatcher("admin/index.jsp");
			dispatcher.forward(request, response);
		}
		else {
			RequestDispatcher dispatcher=request.getRequestDispatcher("admin/adminloginAndRegister.jsp");
			dispatcher.forward(request, response);
		}
	}
	
	protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String username=request.getParameter("username");
		String password1=request.getParameter("password1");
		String password2=request.getParameter("password2");
		if(password1.equals(password2)) {
			User user=new User();
			user.setUsername(username);
			user.setPassword(password1);
			UserService userService=new UserServiceImpl();
			int n=userService.add(user);
			if(n>=1) 
				request.setAttribute("information", "注册成功！");
			else
				request.setAttribute("information", "用户名已存在！");
		}else {
			request.setAttribute("information", "密码不一致！");
		}
		request.setAttribute("url", "admin/adminloginAndRegister.jsp");
		request.setAttribute("second", 3);
		RequestDispatcher dispatcher=request.getRequestDispatcher("admin/tip.jsp");
		dispatcher.forward(request, response);
	}
}
